package com.sky.city.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import androidx.appcompat.widget.Toolbar;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.common.baselibrary.base.BaseFragment;
import com.sky.city.R;
import com.sky.city.adepter.BeautyRvAdepter;
import com.sky.city.adepter.ClassfyRvAdepter;
import com.sky.city.base.BaseApplication;
import com.sky.city.util.LocalDataManagerUtils;
import com.sky.city.widget.AutoHeightViewPager;
import com.sky.city.widget.ViewPagerAdapter;
import com.sky.city.widget.gallery.GridViewAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import cn.jzvd.JzvdStdTikTok;

@SuppressLint("NewApi")
public class HomeFragment extends BaseFragment implements View.OnScrollChangeListener {

    public static HomeFragment getInstance(){
        HomeFragment homeFragment=new HomeFragment();
        return homeFragment;
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.netscrollview)
    NestedScrollView netscrollview;

    @BindView(R.id.search_img)
    ImageView search_img;

    @BindView(R.id.like_img)
    ImageView like_img;

    @BindView(R.id.beautyrv)
    RecyclerView beautyrv;

    @BindView(R.id.jz_video)
    JzvdStdTikTok jzvdStdTikTok;

    @BindView(R.id.classfyrv)
    RecyclerView classfyrv;

    @BindView(R.id.viewpager)
    ViewPager viewpager;


    private BeautyRvAdepter beautyRvAdepter;
    private ClassfyRvAdepter classfyRvAdepter;

    private LayoutInflater inflater;
    private int pageCount;//总页数
    private int pageSize = 4;//每一页的个数
    private int curIndex = 0;//当前显示的事第几页
    private List<View> mPagerList;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_layout;
    }

    @Override
    public void init(View view) {
//        netscrollview.setOnScrollChangeListener(this);
        inflater = LayoutInflater.from(getActivity());
        initBeautyRv();
        initClassRv();
        setvideo();
        setGalleryPicList();
    }


    private void initBeautyRv() {
        beautyRvAdepter = new BeautyRvAdepter();
        beautyrv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        beautyrv.setAdapter(beautyRvAdepter);
        beautyRvAdepter.bindToRecyclerView(beautyrv);
        beautyRvAdepter.setNewData(LocalDataManagerUtils.getInstance().getPicList());
    }

    private void initClassRv(){
        classfyRvAdepter=new ClassfyRvAdepter();
        classfyrv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        classfyrv.setAdapter(classfyRvAdepter);
        classfyRvAdepter.bindToRecyclerView(classfyrv);
        classfyRvAdepter.setNewData(LocalDataManagerUtils.getInstance().getLabelList());
    }

    private void setvideo() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        String proxyUrl = BaseApplication.getProxy(getActivity()).getProxyUrl("https://cdn-5.stacees.co.uk/static/banner/mobile/banner-mobile1.mp4");
        map.put("", proxyUrl);
        JZDataSource jzDataSource = new JZDataSource(map, "");
        jzDataSource.looping = true;
        jzDataSource.currentUrlIndex = 0;
        jzDataSource.headerMap.put("key", "value");
        jzvdStdTikTok.setUp(jzDataSource
                , JzvdStd.NORMAL_ORIENTATION);
        jzvdStdTikTok.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
        jzvdStdTikTok.post(() -> jzvdStdTikTok.startVideo());
    }

    private void setGalleryPicList(){
        pageCount = (int) Math.ceil(LocalDataManagerUtils.getInstance().getImgList().size() * 1.0 / pageSize);

        mPagerList = new ArrayList<>();
        for (int i = 0; i < pageCount; i++) {
            //每个页面都是inflate出的一个新实例
            GridView gridView = (GridView) inflater.inflate(R.layout.gridview, null);
            gridView.setAdapter(new GridViewAdapter(getActivity(), LocalDataManagerUtils.getInstance().getImgList(), i, pageSize));
            mPagerList.add(gridView);
            gridView.setOnItemClickListener((parent, view, position, id) -> {
                int pos = position + curIndex * pageSize;
            });
        }
        //设置viewpageAdapter
        viewpager.setAdapter(new ViewPagerAdapter(mPagerList));

    }

    @Override
    public void onScrollChange(View view, int scrollX, int scrollY, int oldScrollX,
                               int oldScrollY) {
        if(scrollY>toolbar.getBottom()){
            toolbar.setBackgroundColor(Color.parseColor("#ffffff"));
        }else{
            toolbar.setBackgroundColor(Color.TRANSPARENT);
        }
    }

//    private void dongtaiset(){
//        ViewPager viewPager=new ViewPager(getActivity());
//        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
//                FrameLayout.LayoutParams.MATCH_PARENT,
//                FrameLayout.LayoutParams.WRAP_CONTENT);
//        viewPager.setLayoutParams(layoutParams);
//        viewPager.setAdapter(new ViewPagerAdapter(mPagerList));
//        viewPager.post(new Runnable() {
//            @Override
//            public void run() {
//                Log.e("bst====","获取的view高度为"+viewPager.getHeight());
//
//            }
//        });
//        frameLayout.addView(viewPager);
//    }
}
